function resizeKanban(){
	var bodyHeight = $(window).height();
	
	if(bodyHeight < 300){
		$("#sprint_meetings ul").height(bodyHeight - 310);
		$("ul.sprints, .list_items").height(300);
	}else{
		$("#sprint_meetings ul").height(bodyHeight - 131);
		$("ul.sprints, .list_items").height(bodyHeight - 121);
	}
}

$(window).resize(function() {
	resizeKanban();
});

$(document).ready(function() {	
	loadSprints();
	loadMeetingsTypes();
	resizeKanban();

	$("#menu_add").click(function(){
		if($("#toolsbar .add").css("display") == "none"){
			$("#toolsbar").slideUp('fast',function(){
				$(".tools_left > div",this).hide();
				$(".add",this).show();

				$(this).slideDown('slow');
			});
		}
	});

	$("#toolsbar .cancel").click(function(){
		$("#toolsbar").slideUp('fast',function(){
			$(".tools_left > div",this).hide();
			$(".title",this).show();

			$(this).slideDown('slow');
		});
	});
});

function showModal(){
	resizeKanban();
}

function closeModal(){
	resizeKanban();
}


/******************************************************************************************************************
Sprint Functions
******************************************************************************************************************/
function loadSprints(){
	$.ajax({
		url: SITE+"app/Views/V_Meeting.php?f=getSprints",
		async: false,
		success: function(data){
			var obj = JSON.decode(data);
			$("#sprints").html('<div class="list_items right"><a title="Fechar" class="close_box"></a><div id="sprint_meetings"></div></div>'+obj[1]);

			$(".sprints li").droppable({
				hoverClass: "active_drop",
				drop: function( event, ui ) {
					var obj			= $(ui.draggable).children();
					var	idSprint	= $(this).attr("id").replace("sprint_","");

					if($(obj).attr("id").search("meetingType_") == 0)
						createNewMeeting(obj,idSprint,0);
					//else
					//	changeMeeting(obj,$("ul.sprints").data("sprint"),idSprint);
				}
			});			

			$(".sprints li .description").click(function(){
				setSprint($(this).parent());
			});
		}
	});

	return false;
}

function setSprint(obj){
	var	id = $(obj).attr("id").replace("sprint_","");

	if($("ul.sprints").data("sprint") != id){
		loadMeetings(id);
	}else{
		openBoxMeetings(id);
	}
	
	$("ul.sprints").data("sprint",id);
}

function updateSprints(){
	$.ajax({
		url: SITE+"app/Views/V_Meeting.php?f=getMeetingSprints",
		success: function(data){
			var obj = JSON.decode(data);
			var x;
			for(x in obj[1]){				
				$("#sprint_"+obj[1][x].id+" .countMeetings").html(obj[1][x].meetings);
			}
		}
	});
}

function checkSprint(id){
	$(".sprints li").removeClass("selected");
	$("#sprint_"+id).addClass("selected");
}

/******************************************************************************************************************
Reuniões Functions
******************************************************************************************************************/
function loadMeetingsTypes(){
	$.ajax({
		url: SITE+"app/Views/V_Meeting.php?f=getMeetingTypes",
		success: function(data){
			var obj = JSON.decode(data);
			$("#meetings").html(obj[1]);			

			$("#meetings ul li").draggable({
				helper: "clone",
				zIndex: 9999,
				scroll:false,
				appendTo: "body",
				connectToSortable: "#sprint_meetings ul",
				start: function(event, ui) {					
					$(".details",$(ui.helper)).css("display","none");
					$(".details",$(this)).css("display","none");
				}
			});

			$("#meetings button.expand").click(function(){
				var obj = $(this);
				$(".details",$(this).parent().parent()).slideToggle("fast",function(){
					if($(this).css("display") == "none"){
						obj.html("+");
					}else{
						obj.html("-");
					}
				});
			});
		}
	});
}

function loadMeetings(idSprint){
	$.ajax({
		url: SITE+"app/Views/V_Meeting.php?f=getMeetings&idSprint="+idSprint,
		async: false,
		success: function(data){
			var obj = JSON.decode(data);
			$("#sprint_meetings").html(obj[1]);

			$("#sprint_meetings ul").height($(".list_items.right").height()-10);

			$("#sprint_meetings > ul").sortable({			
				placeholder: 'placeholder',
				start: function(event, ui) {
					$(".details",$(ui.item)).css("display","none");
					$(".buttons",$(ui.item)).css("display","none");
				},
				beforeStop: function(event, ui) {
					var idMeetingType = $(ui.item).children().attr("id").replace("meetingType_","");

					createNewMeeting($(ui.item).children(), $(".sprints").data("sprint"));

					if(idMeetingType > 0)
						ui.item.remove(); 
				}
			});

			$("#sprint_meetings button.expand").click(function(){
				var obj = $(this);
				$(".details",$(this).parent().parent()).slideToggle("fast",function(){
					if($(this).css("display") == "none"){
						obj.html("+");
					}else{
						obj.html("-");
					}
				});
			});
			

			$(".list_items.right .close_box").unbind("click");
			$(".list_items.right .close_box").click(function(){				
				$(this).hide();
				$(".list_items.right").hide("slide",{direction:"right"});
				$(".sprints").width("auto");
			});

			$("#sprint_meetings .title_bar").click(function(){
				$("> .buttons",$(this).parent()).slideToggle("fast");
			});

			$("#sprint_meetings .title_bar").dblclick(function(){
				var ids = $(this).attr("id").replace("meeting_","");
				ids = ids.split("-");
				editMeeting(ids[0],ids[1]);
			});
			
			openBoxMeetings(idSprint);
		}
	});
}

function loadHTMLMeeting(obj){
	if(obj[0]){
		openBox("overlay", obj[1]);

		$("#formMeeting").validate({
			submitHandler: function(form) {
				var query = $(form).serialize();
				
				var meetingType = $("#formMeeting :input[name='idMeetingType']").val();

				$.ajax({
					url: SITE+"app/Views/V_Meeting.php?f=saveMeeting&"+query,
					async: false,
					success: function(data){
						var obj = JSON.decode(data);
						resultMensage(obj, true);
						loadMeetings($("ul.sprints").data("sprint"));
						updateSprints();

						if(meetingType == 2)
							usersMeeting(obj[2]);
					}
				});			
			}
		});

		var obj	 = $(".date-pick").dateinput();
		$("#formMeeting #date").data("dateinput").setMin(DATE_TODAY);

	}else{
		resultMensage(obj, true);
	}
}

function createNewMeeting(obj, idSprint){
	var idMeetingType = $(obj).attr("id").replace("meetingType_","");

	if(idMeetingType > 0 && idSprint > 0){
		setSprint($("#sprint_"+idSprint));

		$.ajax({
			url: SITE+"app/Views/V_Meeting.php?f=newMeeting&idMeetingType="+idMeetingType+"&idSprint="+idSprint,
			success: function(data){
				var obj = JSON.decode(data);
				loadHTMLMeeting(obj);
			}
		});
	}
}


function editMeeting(idMeeting, idSprint){
	if(idMeeting > 0 && idSprint > 0){
		$.ajax({
			url: SITE+"app/Views/V_Meeting.php?f=editMeeting&idMeeting="+idMeeting+"&idSprint="+idSprint,
			success: function(data){
				var obj = JSON.decode(data);
				loadHTMLMeeting(obj);
			}
		});
	}
}

function deleteMeeting(idMeeting){
	openBox("modalDelete");

	$("#modalDelete #confirm").click(function(){
		$.ajax({
			url: SITE+"app/Views/V_Meeting.php?f=deleteMeeting&idMeeting="+idMeeting,
			success: function(data){
				var obj = JSON.decode(data);
				if(obj[0]){
					$("#meeting_"+idMeeting).remove();
					updateSprints();
				}
				resultMensage(obj, true);
			}
		});
	});
}

function changeMeeting(idMeeting,idSprint,idNewSprint){
	$.ajax({
		url: SITE+"app/Views/V_Meeting.php?f=changeMeeting&idMeeting="+idMeeting+"&idSprint="+idSprint+"&idNewSprint="+idNewSprint,
		success: function(data){
			var obj = JSON.decode(data);
			if(obj[0]){
				$("#meeting_"+idMeeting+"-"+idSprint).remove();
				updateSprints();
			}
			resultMensage(obj, true);
		}
	});
}

function openBoxMeetings(idSprint){	
	var objListItems = $(".list_items.right");

	$(".sprints li").removeClass("selected");
	$("#sprint_"+idSprint).addClass("selected");

	if(objListItems.css("display") == "none"){
		objListItems.show("slide",{direction:"right"},function(){
			$(".list_items.right .close_box").show();

			var boxSprints = $(".sprints").width();
			$(".sprints").width(boxSprints-268);
		});
	}else{
		$(".close_box",objListItems).show();
	}
}

/******************************************************************************************************************
Reuniões de Acompanhamento - Functions
******************************************************************************************************************/
function usersMeeting(idMeeting){
	if(idMeeting > 0){
		$.ajax({
			url: SITE+"app/Views/V_Meeting.php?f=newUserMeeting&idMeeting="+idMeeting,
			success: function(data){
				var obj = JSON.decode(data);
				loadHTMLUserMeeting(obj);
			}
		});
	}
}

function loadHTMLUserMeeting(obj){
	if(obj[0]){
		openBox("overlay", obj[1]);

		$("#formUserMeeting").validate({
			submitHandler: function(form) {
				var query = $(form).serialize();
				
				$.ajax({
					url: SITE+"app/Views/V_Meeting.php?f=saveUserMeeting&"+query,
					async: false,
					success: function(data){
						var obj = JSON.decode(data);
						resultMensage(obj, false);
					}
				});
			}
		});

		$("#formUserMeeting :input[name='idUser']").change(function(){
			var idUser		= $(this).val();
			var idMeeting	= $("#formUserMeeting :input[name='idMeeting']").val();
			
			$("#formUserMeeting #done").val("");
			$("#formUserMeeting #doing").val("");
			$("#formUserMeeting #impediments").val("");
			$("#formUserMeeting #description").val("");

			if(idUser != "" && idMeeting > 0){
				$.ajax({
					url: SITE+"app/Views/V_Meeting.php?f=getUserMeeting&idUser="+idUser+"&idMeeting="+idMeeting,
					async: false,
					success: function(data){
						var obj = JSON.decode(data);

						if(obj[0]){
							$("#formUserMeeting #done").val(obj[1].done);
							$("#formUserMeeting #doing").val(obj[1].doing);
							$("#formUserMeeting #impediments").val(obj[1].impediments);
							$("#formUserMeeting #description").val(obj[1].description);
						}else if(obj[1] != ""){
							resultMensage(obj, true);
						}
						
					}
				});
			}							
		});
	}else{
		resultMensage(obj, true);
	}
}